/**
 * 
 */
package com.gragra.sampling.nonproj.structures.nonprojective;
import java.util.BitSet;
import com.gragra.sampling.PopulationProbabilityAssigner;
import com.gragra.sampling.ThreadedUniformAccess;
import com.gragra.sampling.binary.ConvertSample;
import com.gragra.sampling.sequentialStructure.WordAndTagHolder;
/**
 * @author Christoph Teichmann
 * 11.10.2013
 */
public class MHNonProjDepdTree extends NonProjDepdTree
{
	/**
	 * @param wth
	 * @param threads
	 * @param cs
	 */
	public MHNonProjDepdTree(WordAndTagHolder wth, int threads, ConvertSample cs)
	{super(wth, threads, cs);}
	/* (non-Javadoc)
	 * @see com.gragra.sampling.nonproj.structures.nonprojective.NonProjDepdTree#resampleRoot(com.gragra.sampling.PopulationProbabilityAssigner, double, int, com.gragra.sampling.ThreadedUniformAccess)
	 */
	@Override
	protected void resampleRoot(PopulationProbabilityAssigner pa,
			double annealing, int thread, ThreadedUniformAccess tua)
	{}
	/* (non-Javadoc)
	 * @see com.gragra.sampling.nonproj.structures.nonprojective.NonProjDepdTree#resampleAtPosition(int, com.gragra.sampling.PopulationProbabilityAssigner, double, int, com.gragra.sampling.ThreadedUniformAccess)
	 */
	@Override
	protected void resampleAtPosition(int pos,
			PopulationProbabilityAssigner pa, double annealing, int thread,
			ThreadedUniformAccess tua)
	{
		double oldProb = this.makeLogProbForThisConsideringAnnealing(pa, annealing, thread);
		int oldHead = this.getHeadPosition(pos, thread);
		BitSet bs = this.makeAllHeadCandidates(pos, thread, pa);
		int choice = tua.nextInt(thread, 1, bs.cardinality());
		int newHead = -1;
		for(int i=0;i<choice;++i)
		{newHead = bs.nextSetBit(newHead+1);}
		this.setNewHead(pos, newHead, thread);
		double newProb = this.makeLogProbForThisConsideringAnnealing(pa, annealing, thread);
		if(Math.log(tua.nextDouble(thread)) > newProb-oldProb)
		{this.setNewHead(pos, oldHead, thread);}
	}
}